<?php

namespace App\Http\Controllers\Index;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\libs\sms\api_demo\SmsDemo;
use App\Models\Vip;
use Illuminate\Support\Facades\DB;
use Image;

class VipController extends Controller
{
    // 登录页面
    public function login(){
        return view('index/vip/login');
    }

    // 发送验证码
    public function message(Request $request){
        // 接收传参
        $phone = $request->phone;
        // 设置六位数的随机验证码
        $captcha = mt_rand(000000,999999);
        // 把$phone和$code存入session中
        session(["phone" => $phone]);
        session(["captcha" => $captcha]);
        // 调用示例：
        $response = SmsDemo::sendSms($phone,$captcha);

        // $response = SmsDemo::sendSms($phone,$captcha);
        // echo "发送短信(sendSms)接口返回的结果:\n";
        // print_r($response);
    }

    // 执行注册
    public function add(Request $request, $length = 6){
        // =======================随机生成用户昵称============================== //
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
        $username = "";
        for ( $i = 0; $i < $length; $i++ )  {
            //取字符数组 $chars 的任意元素
            $username .= $chars[ mt_rand(0, strlen($chars) - 1) ];
        }
        // 接受传参
        $phone = $request->phone;
        $captcha = $request->captcha;
        $password = md5($request->password);

        // 接受session值
        $sessioncaptcha = session("captcha");
        $sessionphone = session("phone");

        //根据phone去查询是否与数据库重复
        $stu = Vip::where('phone', $phone)->first();
        // 判断手机号是否是唯一
        if ($stu == null) {
            // 判断captcha值是否与sessioncaptcha值一致
            if ($phone == $sessionphone) {
                if ($captcha == $sessioncaptcha) {
                    // 执行添加
                    $arr = new Vip;
                    $arr->username = $username;
                    $arr->phone = $phone;
                    $arr->password = $password;
                    $stu = $arr->save();
                    if($stu) {
                        return 4;
                    }
                    return 2;
                }else{
                    return 0;
                }
            }else{
                return 1;
            }
        }else{
            return 3;
        }
    }

    // 验证手机号是否重复
    public function yanzheng(Request $request){
        // 接受传参
        $phone = $request->phone;
        // 根据phone去查询是否与数据库重复
        $stu = Vip::where('phone', $phone)->first();
        // 判断手机号是否是唯一
        if ($stu == null) {
            return 1;
        }else{
            return 2;
        }
    }

    //执行登录
    public function login2(Request $request){
        // $input = $request->all();
        // return($input);

        // 接受传参
        $phone = $request->phone;
        $password = md5($request->password);
        // return(md5($request->password));

        // 根据phone去查询
        $stu = Vip::where("phone",$phone)->first();
        // 判断用户是否存在
        if ($stu) {
            if($stu->state==1){
                // 判断密码是否一致
                if ($stu->password == $password) {
                    // 保存session值
                    session(["phone" => $phone]);
                    session(['uid' => $stu->id]);
                    return 2; //正确
                }else{
                    return 1; //密码错误
                }
            }else{
                return 3;
            }
        }else{
            return 0; //不存在
        }
    }

    //忘记密码
     public function zhpassword(request $request){
        // $input = $request->all();
        // return($input);
        // 接收传参
        $captcha = $request->captcha;
        $password = $request->password;
        // 接受session值
        $sessioncaptcha = session("captcha");
        $sessionphone = session("phone");

        // 判断captcha值是否与sessioncode值一致
        // if ($captcha == $sessioncaptcha) {
            // 执行修改
            $stu = Vip::where('phone', $sessionphone)
                ->update(['password' => md5($password)]);
                // return($stu);
            // 判断是否修改成功
            if ($stu) {
                return 1;
            }else{
                return 0;
            }
        // }else{
        //     return 0;
        // }
    }

    // 退出
    public function quit(Request $request){
        $request->session()->forget('phone');
        $request->session()->forget('uid');
        return redirect('/');
    }

	// 个人中心
	public function udai_welcome(){
    	return view('index/vip/udai_welcome');
    }
    // 00b7691d86d96aebd21dd9e138f90840  ------111222
    // d41d8cd98f00b204e9800998ecf8427e ----123123
    // 96e79218965eb72c92a549dd5a330112 -----111111
    // 个人信息设置
    public function udai_setting(){
        $user=DB::table('vip')->find(session('uid'));
        // dd($user);
        if($user->haddr){
           $address=explode(' ',$user->haddr);
        }else{
            $address=['山西省','运城市','盐湖区',''];
        }
    	return view('index/vip/udai_setting',['user'=>$user,'address'=>$address]);
    }
    // 个人信息更改
    public function userinfo_update(Request $request){
        // dd($request->all());
        $haddr=$request->province.' '.$request->city.' '.$request->area.' '.$request->town;
        $data['username']=$request->username;
        $data['sex']=$request->sex;
        $data['birthday']=$request->birthday;
        $data['haddr']=$haddr;
        DB::table('vip')->where('id',session('uid'))->update($data);
        return redirect()->action('index\VipController@udai_setting',['data'=>1]);
        
    }
    // 头像修改
    public function user_icon_update(Request $request){
        // return 0;
        // dd($request->file('icon'));
        $img=Image::make($request->file('icon'));
        $rdm=time().mt_rand(1000,9999);
        $imgname=$rdm.'.jpg';
        if(DB::table('vip')->where('id',session('uid'))->update(['icon'=>$imgname])){
            $img->save('upload/'.$imgname);
            return redirect()->action('index\VipController@udai_setting');
        }

    }
    // 修改支付密码
    public function udai_paypwd_modify(){
    	return view('index/vip/udai_paypwd_modify');
    }
    // 修改登录密码
    public function udai_pwd_modify(){
    	return view('index/vip/udai_pwd_modify');
    }
    // 验证旧密码
    public function modifypwd_step1(Request $request){
        if(DB::table('vip')->where('id',session('uid'))->value('password')==md5($request->pwd)){
            return redirect('/index/vip/udai_pwd_modify?step=2');
        }else{
            return redirect('/index/vip/udai_pwd_modify?step=1&error=1');
        }
    }
    // 更改新密码
    public function modifypwd_step2(Request $request){
        // dd($request->all());
        if(DB::table('vip')->where('id',session('uid'))->update(['password'=>md5($request->pwd)])){
            $request->session()->forget('phone');
            $request->session()->forget('uid');
            return redirect('/index/vip/udai_pwd_modify?step=3');
        }else{
            return redirect('/index/vip/udai_pwd_modify?step=2&error=2');
        }
    }

    // 收货地址
    public function udai_address(){
    	 $arr=DB::table('shipping')->where('uid',session('uid'))->get();
        return view('index/vip/udai_address',['arr'=>$arr]);
    }

    //添加收货地址
    public function udai_address_insert(Request $request){
        // dd($request->all());
        $address=$request->province.' '.$request->city.' '.$request->area.' '.$request->town;
        $data['uid']=session('uid');
        $data['cnee']=$request->name;
        $data['address']=$address;
        $data['details']=$request->details;
        $data['tel']=$request->tel;
        if (DB::table('shipping')->insert($data)){
            return 1;
        }else{
            return 0;
        }
    }

    //删除收货地址
    public function udai_address_del(Request $request){
        if (DB::table('shipping')->whereIn('id',$request->ids)->delete()) {
            return 1;
        }else{
            return 0;
        }
    } 

    // 修改收货地址
    public function udai_address_edit(Request $request){
    	$piece=DB::table('shipping')->find($request->id);
        $address=explode(' ',$piece->address);
        return view('/index/vip/udai_address_edit',['piece'=>$piece,'address'=>$address]);
    }

    //修改
    public function udai_address_update(Request $request){
        // dd($request->all());
        $address=$request->province.' '.$request->city.' '.$request->area.' '.$request->town;
        $data['cnee']=$request->name;
        $data['address']=$address;
        $data['details']=$request->details;
        $data['tel']=$request->tel;
        if(DB::table('shipping')->where('id',$request->id)->update($data)){
            return 1;
        }else{
            return 0;
        }
    }


    
}
